package com.pacy.pacybaseproject.net;


import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.goldze.mvvmhabit.utils.AppUtil;
import me.goldze.mvvmhabit.utils.JsonUtil;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * @author PACY
 * 创建时间 19/1/22 17:57.
 * 邮箱 pengwwe457621023@qq.com
 * 类描述   构造公共参数工具类
 */
public class HttpUtils {
    //当前版本号
    public static String version = AppUtil.getVersionCode() + "";
    //平台
    public static String os = "Android";


    public static final String MULTIPART_FORM_DATA = "multipart/form-data";


    /**
     * 构建参数
     *
     * @param params 除公共参数以外的具体参数
     * @return
     */
    public static Map<String, Object> buildParams(Map<String, Object> params) {
        Map<String, Object> allParams = new HashMap<>();

//        //如果token不为空则添加token到参数列表
//        if (StringUtil.isNotEmpty((String) PreferencesUtil.get("token", ""))) {
//            allParams.put("userId", PreferencesUtil.get("token", ""));
//            allParams.put("client", "android");
//        }
        if (params != null) {
            allParams.putAll(params);
        }
        return allParams;
    }


    /**
     * 构建form参数  使用表单上传  aoligei
     *
     * @param params
     * @return
     */
    public static RequestBody buildBody(Map<String, Object> params) {

        Map<String, Object> allParams = new HashMap<>();
        if (params != null) {
            allParams.putAll(params);
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type:application/json"), JsonUtil.mapToJson(params));
        return requestBody;
    }

    /**
     * 构建form参数  使用表单上传  aoligei
     *
     * @param params
     * @return
     */
    public static RequestBody buildBody(String params) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type:application/json"), params);
        return requestBody;
    }

    /**
     * 构建form参数  使用表单上传  aoligei
     *
     * @return
     */
    public static RequestBody buildBody() {
        return buildBody("");
    }


    /**
     * 上传图片
     *
     * @param path
     * @return
     */
    public static List<MultipartBody.Part> buildUpLoad(String path) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        File file = new File(path);
        RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), file);
        builder.addFormDataPart("file", file.getName(), requestBody); //添加图片数据，body创建的请求体
//        builder.addFormDataPart("URL", "http://172.16.252.42:8864/hyperlpr3");
        List<MultipartBody.Part> parts = builder.build().parts();

        return parts;
    }

    /**
     * 上传图片参数
     *
     * @param id
     * @return
     */
    public static Map<String, RequestBody> buildImageBody(String id) {
        Map<String, RequestBody> params = new HashMap<>();

        params.put("imgtype", RequestBody.create(
                MediaType.parse(MULTIPART_FORM_DATA), id));

        return params;
    }
}
